Package learn.qual

Source Code of learn.qual.FairWarning

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package learn.qual;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.Scanner;

/**
*
* @author robit
*/
public class FairWarning {

//    static int gcd(int a,int b){
//        while(a!=0){
//         //want a <- b%a, b <- a
//         //first do a <- b, b <- a, followed by a<-a%b
//            a=a+b;
//            b=a-b;
//            a=a-b;
//            a=a%b;
//        }
//        return b;
//    }
//
//    static int gcd(int[] args){
//        int gcd=0;
//        for(int i=0;i<args.length;i++){
//            gcd=gcd(gcd,args[i]);
//        }
//        return gcd;
//    }
//    static int reckon(int []args) {
//        if(args.length<=1) return 0;
//        else{
//            int [] subargs=new int[args.length-1];
//            for(int i=0;i<subargs.length;i++){
//             subargs[i]=args[i]-args[args.length-1];
//            }
//            int divisor = gcd(subargs);
//            int res = args[args.length-1]%divisor;
//            return (res>0)?divisor-res:0;
//
//        }
//    }

    static BigInteger gcd(BigInteger[] args){
        BigInteger gcd=BigInteger.ZERO;
        for(int i=0;i<args.length;i++){
            gcd = gcd.gcd(args[i]);
        }
        return gcd;
    }


    static BigInteger reckon(BigInteger []args) {
        if(args.length<=1) return BigInteger.ZERO;
        else{
            BigInteger [] subargs=new BigInteger[args.length-1];
            for(int i=0;i<subargs.length;i++){
             subargs[i]=args[i].subtract(args[args.length-1]);
            }
            BigInteger divisor = gcd(subargs);
           
            return divisor.subtract(args[args.length-1]).mod(divisor);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        Scanner s = null;
        PrintWriter o = null;
        try {
            s = new Scanner(
                    new BufferedReader(new FileReader("B-large-practice.in")));
            //s.useLocale(Locale.US);
            o =
                    new PrintWriter(new BufferedWriter(new FileWriter("B-large-practice.out")));

            int lineNums = s.nextInt();
            for (int i = 1; i <= lineNums; i++) {
                int argNums = s.nextInt();
                BigInteger argsSet[] = new BigInteger[argNums];
                for (int j = 0; j < argNums; j++) {
                    argsSet[j] = s.nextBigInteger();
                }
                BigInteger answer = reckon(argsSet);
                o.println("Case #" + i + ": " + answer);
            }
        } finally {
            s.close();
            o.close();
        }
    }
}
TOP

Related Classes of learn.qual.FairWarning

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.